<?php

namespace App\Criteria\Clbum\Grade;

use App\Libs\Tools\DataTables;
use Illuminate\Support\Facades\Input;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

class GradeBaseQueryCriteria implements CriteriaInterface{

    public function apply($model, RepositoryInterface $repository){
        $model=$model->leftJoin('users','grade.leader_user_id','=','users.id')
                     ->select('grade.*','users.name as leader_name');
        $name=Input::get('name');
        $desc=Input::get('desc');
        if($name){$model=$model->where('grade.name','like','%'.$name.'%');}
        if($desc){$model=$model->where('grade.desc','like','%'.$desc.'%');}
        $order_column_tag=DataTables::get_order_column_tag();
        if(!is_null($order_column_tag)){
            $model->orderBy($order_column_tag['order_column'],$order_column_tag['order_type']);
        }
        return $model;
    }
}
